package com.doeis.tools;

import java.security.MessageDigest;

import javax.servlet.http.HttpServletRequest;

/**
 * 字符串工具类
 * 
 * @author xie_yan
 *
 */
public class StringHelper {

	/**
	 * 判断字符串是否是数字
	 * 
	 * @author xieyan
	 * @param srcStr
	 *            String, 要判断的源字符串
	 * @return true, 是数字；false, 不是数字
	 */
	public static boolean isDigit(String srcStr) {
		if (srcStr == null) {
			return false;
		}
		int srcStrLenght = srcStr.length();
		if (srcStrLenght <= 0) {
			return false;
		}
		for (int i = 0; i < srcStrLenght; i++) {
			if (i == 0 && srcStr.charAt(i) == '-') {
				continue;
			} else if ((i != srcStrLenght - 1) && srcStr.charAt(i) == '.') {
				continue;
			}
			if (!Character.isDigit(srcStr.charAt(i))) {
				return false;
			}
			return true;
		}
		return false;
	}

	/***
	 * 切断字符串 （没有省略号）
	 * 
	 * @param input
	 * @param length
	 * @return
	 */
	public static String formatStringNoPoint(String input, int length) {
		String temp = "<SPAN title=\"" + input + "\">";
		temp += StringHelper.formatString(input, length);
		temp += "</SPAN>";
		return temp;
	}

	/**
	 * 切断字符串
	 * 
	 * @param msg
	 * @param fixLength
	 * @return
	 */
	public static String formatString(String msg, int fixLength) {
		if (msg == null)
			msg = "";
		if (msg.trim().equals("null") || msg.trim().equals(""))
			msg = "";
		byte data[] = msg.getBytes();
		if (data.length <= fixLength)
			return msg;
		byte tmp[] = new byte[fixLength];
		System.arraycopy(data, 0, tmp, 0, fixLength);
		data = tmp;
		int count = 0;
		for (int i = 0; i < data.length; i++) {
			if (data[data.length - 1 - i] >= 0)
				break;
			count++;
		}

		switch (count % 2) {
		case 1: // '\001'
			byte tmp2[] = new byte[data.length - 1];
			System.arraycopy(data, 0, tmp2, 0, data.length - 1);
			data = tmp2;
			break;
		}
		String reSult = new String(data);
		return reSult;
	}

	/**
	 * 在页面上显示时把空字符串或null转化成&nbsp;
	 * 
	 * @param src
	 * @return
	 */
	public static String getNullToNbsp(String src) {
		String result = "";
		if (toTrim(src).equals("")) {
			result = "&nbsp;";
		} else {
			result = src;
		}
		return result;
	}

	public static String toTrim(String src) {
		if (src == null) {
			return "";
		}
		return src.trim();
	}

	public static String getZeroToLine(int num) {
		String result = "";
		if (num == 0) {
			result = "-";
		} else {
			result = num + "";
		}
		return result;
	}

	/**
	 * 将模板中的变量替换成相应的栏目数据
	 * 
	 * @param variable
	 * @param vlue
	 * @return
	 */
	public static String replaceValue(String templateContent, String variable, String value) {
		variable = "${" + variable + "}$";
		templateContent = templateContent.replace(variable, value);
		return templateContent;
	}

	/**
	 * 取加密字符串
	 * 
	 * @param key
	 * @return
	 * @data:2011-12-15上午9:11:06
	 * @author:wxy
	 */
	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		System.out.println(StringHelper.MD5("123456"));

	}

	public static boolean isNull(String tableName) {
		return toTrim(tableName).length() == 0;
	}

	public static boolean isNotNull(String tableName) {
		return toTrim(tableName).length() > 0;
	}

	public static String show(String value, String defaultValue) {
		if (isNotNull(value)) {
			return value;
		}
		return defaultValue;
	}

	/**
	 * 取客户端连接的ip
	 * 
	 * @Title: getRequestIp
	 * @data:2016-3-27下午4:18:43
	 * @author:zhl
	 * 
	 * @return
	 */
	public static String getRequestIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "127.0.0.1";
		}
		return ip;
	}
}
